﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Linq;
using M7;

namespace C7
{
    /// <summary>
    /// Interaction logic for CurrentUser.xaml
    /// </summary>
    public partial class CurrentUser : UserControl
    {
        public CurrentUser()
        {
            this.InitializeComponent();
        }

        #region ImageSourceProperty
        public ImageSource ImageSource
        {
            get
            {
                return (ImageSource)GetValue(ImageSourceProperty);
            }
            set
            {
                SetValue(ImageSourceProperty, value);
            }
        }

        public static readonly DependencyProperty ImageSourceProperty =
        DependencyProperty.Register(
            "ImageSource",
            typeof(ImageSource),
            typeof(CurrentUser),
            new FrameworkPropertyMetadata(
                new PropertyChangedCallback(ChangeImage)));

        private static void ChangeImage(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            (source as CurrentUser).UpdateImage(e.NewValue as ImageSource);
        }

        private void UpdateImage(ImageSource NewImageSource)
        {
            imgCurrentUser.Source = NewImageSource;
        }
        #endregion

        #region UserNameProperty
        public string UserName
        {
            get
            {
                return (string)GetValue(UserNameProperty);
            }
            set
            {
                SetValue(UserNameProperty, value);
            }
        }

        public static readonly DependencyProperty UserNameProperty =
        DependencyProperty.Register(
            "UserName",
            typeof(string),
            typeof(CurrentUser),
            new FrameworkPropertyMetadata(
                new PropertyChangedCallback(ChangeUserName)));

        private static void ChangeUserName(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            (source as CurrentUser).UpdateUserName(e.NewValue as string);
        }
        private void UpdateUserName(string NameUserName)
        {
            tbUserName.Text = NameUserName;
        }
        #endregion

        #region TextSizeProperty
        public double TextSize
        {
            get
            {
                return (double)GetValue(TextSizeProperty);
            }
            set
            {
                SetValue(TextSizeProperty, value);
            }
        }
        public static readonly DependencyProperty TextSizeProperty =
        DependencyProperty.Register(
            "TextSize",
            typeof(double),
            typeof(CurrentUser),
            new FrameworkPropertyMetadata(
                new PropertyChangedCallback(ChangeTextSize)));

        private static void ChangeTextSize(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            (source as CurrentUser).UpdateTextSize((double)e.NewValue);
        }

        private void UpdateTextSize(double NewTextSize)
        {
            tbUserName.FontSize = NewTextSize;
        }
        #endregion

        private void CurrentUserControl_MouseDown(object sender, MouseButtonEventArgs e)
        {
            //this.Tag
            //this.UserName

            foreach (FrameworkElement i in Info.UsersPanel.Children)
            {
                if (i == this)
                    i.Margin = new Thickness(4, 0, 0, 0);
                if (i != this && i != Info.window.imgAdd)
                    i.Margin = new Thickness(4, 0, 0, 24);
            }
            var CustomerIDs = from cust in Info.current.customers
                              where cust.Username == this.tbUserName.Text
                              select cust;
            foreach (var item in CustomerIDs)
            {
                Info.current.CustomerID = item.CustomerID;
                break;
            }
            Msg SendMsg = new Msg(Info.current.Port, Info.current.TableID);
            SendMsg.enumMethod = MessageBoxButton.OK;
            SendMsg.obj = Info.current.CustomerID;
            TcpConnection.SendingQueue.Enqueue(SendMsg);
        }

    }
}